package com.cn.admin.controller;

import com.cn.admin.vo.coupon.CouponVO;
import com.cn.admin.vo.coupon.TrackVO;
import com.cn.api.CouponFeignClient;
import com.cn.common.result.Result;
import com.cn.dto.CouponDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "后台管理优惠券")
@RestController
@RequestMapping("/back/coupon")
public class CouponController {

    @Resource
    private CouponFeignClient couponFeignClient;

    @ApiOperation(value = "查询所有优惠券")
    @PostMapping("/findAllCoupon")
    @ResponseBody
    public Result<List<CouponVO>> findAllCoupon(CouponVO couponVO){

        List<CouponVO> couponVOList = new ArrayList<>();
        CouponDTO couponDTO = new CouponDTO();
        List<CouponDTO> list = couponFeignClient.findAllCoupon(couponDTO);
        /**
         * vo转dto
         */
        for (CouponDTO couponDTO1:list){
            CouponVO couponVO1 = new CouponVO();
            couponVO1.setId(couponDTO1.getId());
            couponVO1.setName(couponDTO1.getName());
            couponVO1.setType(couponDTO1.getType());
            couponVO1.setCoupon_id(couponDTO1.getCoupon_id());
            couponVO1.setImg(couponDTO1.getImg());
            couponVO1.setCount(couponDTO1.getCount());
            couponVO1.setStatus(couponDTO1.getStatus());
            couponVO1.setCreate_time(couponDTO1.getCreate_time());
            couponVO1.setUpdate_time(couponDTO1.getUpdate_time());
            couponVO1.setStart_time(couponDTO1.getStart_time());
            couponVO1.setEnd_time(couponDTO1.getEnd_time());
            couponVO1.setCoupon_amount(couponDTO1.getCoupon_amount());
            couponVO1.setExplain(couponDTO1.getExplain());
            couponVO1.setEffective_time(couponDTO1.getEffective_time());

            couponVOList.add(couponVO1);
        }

        return Result.success(couponVOList);
    }

    @ApiOperation(value = "根据ID查询优惠券")
    @PostMapping("/findById")
    @ResponseBody
    public Result<CouponVO> findById(int id){

        CouponDTO couponDTO = new CouponDTO();
        CouponVO couponVO = new CouponVO();
        couponDTO = couponFeignClient.findById(id);

        couponVO.setId(couponDTO.getId());
        couponVO.setCoupon_id(couponDTO.getCoupon_id());
        couponVO.setImg(couponDTO.getImg());
        couponVO.setName(couponDTO.getName());
        couponVO.setType(couponDTO.getType());
        couponVO.setCount(couponDTO.getCount());
        couponVO.setStatus(couponDTO.getStatus());
        couponVO.setCreate_time(couponDTO.getCreate_time());
        couponVO.setUpdate_time(couponDTO.getUpdate_time());
        couponVO.setStart_time(couponDTO.getStart_time());
        couponVO.setEnd_time(couponDTO.getEnd_time());
        couponVO.setCoupon_amount(couponDTO.getCoupon_amount());
        couponVO.setExplain(couponDTO.getExplain());
        couponVO.setEffective_time(couponDTO.getEffective_time());

        return Result.success(couponVO);
    }

    @ApiOperation(value = "根据状态查询优惠券")
    @PostMapping("/findByStatus")
    @ResponseBody
    public Result<CouponVO> findByStatus(int status){
        CouponDTO couponDTO = new CouponDTO();
        CouponVO couponVO = new CouponVO();
        couponDTO = couponFeignClient.findByStatus(status);

        couponVO.setId(couponDTO.getId());
        couponVO.setCoupon_id(couponDTO.getCoupon_id());
        couponVO.setImg(couponDTO.getImg());
        couponVO.setName(couponDTO.getName());
        couponVO.setType(couponDTO.getType());
        couponVO.setCount(couponDTO.getCount());
        couponVO.setStatus(couponDTO.getStatus());
        couponVO.setCreate_time(couponDTO.getCreate_time());
        couponVO.setUpdate_time(couponDTO.getUpdate_time());
        couponVO.setStart_time(couponDTO.getStart_time());
        couponVO.setEnd_time(couponDTO.getEnd_time());
        couponVO.setCoupon_amount(couponDTO.getCoupon_amount());
        couponVO.setExplain(couponDTO.getExplain());
        couponVO.setEffective_time(couponDTO.getEffective_time());

        return Result.success(couponVO);
    }

    @ApiOperation(value = "创建优惠券")
    @PostMapping("/createCoupon")
    @ResponseBody
    public Result createCoupon(CouponVO couponVO){
        CouponDTO couponDTO = new CouponDTO();
        couponDTO.setCoupon_id(couponVO.getCoupon_id());
        couponDTO.setImg(couponVO.getImg());
        couponDTO.setName(couponVO.getName());
        couponDTO.setType(couponVO.getType());
        couponDTO.setCount(couponVO.getCount());
        couponDTO.setStatus(couponVO.getStatus());
        couponDTO.setCreate_time(couponVO.getCreate_time());
        couponDTO.setUpdate_time(couponVO.getUpdate_time());
        couponDTO.setStart_time(couponVO.getStart_time());
        couponDTO.setEnd_time(couponVO.getEnd_time());
        couponDTO.setCoupon_amount(couponVO.getCoupon_amount());
        couponDTO.setExplain(couponVO.getExplain());
        couponDTO.setEffective_time(couponVO.getEffective_time());

        int coupon = couponFeignClient.createCoupon(couponDTO);
        return Result.success(null,"创建成功");
    }

    @ApiOperation(value = "修改优惠券")
    @PostMapping("/editCoupon")
    @ResponseBody
    public Result editCoupon(CouponVO couponVO){
        CouponDTO couponDTO = new CouponDTO();
        couponDTO.setId(couponVO.getId());
        couponDTO.setImg(couponVO.getImg());
        couponDTO.setName(couponVO.getName());
        couponDTO.setType(couponVO.getType());
        couponDTO.setCount(couponVO.getCount());
        couponDTO.setStart_time(couponVO.getStart_time());
        couponDTO.setEnd_time(couponVO.getEnd_time());
        couponDTO.setCoupon_amount(couponVO.getCoupon_amount());
        couponDTO.setExplain(couponVO.getExplain());
        couponDTO.setEffective_time(couponVO.getEffective_time());
        Integer integer = couponFeignClient.editCoupon(couponDTO);
        return Result.success(1,"修改成功");
    }

    @ApiOperation(value = "删除优惠券")
    @PostMapping("/deleteCoupon")
    @ResponseBody
    public Result deleteCoupon(int id){
        Integer integer = couponFeignClient.deleteCoupon(id);
        return Result.success(integer,"删除成功");
    }

    @ApiOperation(value = "优惠券活动开始/结束")
    @PostMapping("/enable")
    @ResponseBody
    public Result enable(int id){
        Integer enable = couponFeignClient.enable(id);
        return Result.success(enable,"已启用/已停用");
    }

    @ApiOperation(value = "优惠券追踪")
    @PostMapping("/findAllDetailed")
    @ResponseBody
    public Result<List<TrackVO>> findAllDetailed(TrackVO trackVO){
        CouponDTO couponDTO = new CouponDTO();
        List<TrackVO> list = new ArrayList<>();
        List<CouponDTO> list1 = couponFeignClient.findAllDetailed(couponDTO);
        for (CouponDTO couponDTO1: list1){
            TrackVO trackVO1 = new TrackVO();
            trackVO1.setId(couponDTO1.getId());
            trackVO1.setCoupon_id(couponDTO1.getCoupon_id());
            trackVO1.setUser_id(couponDTO1.getUser_id());
            trackVO1.setType(couponDTO1.getType());
            trackVO1.setGet_time(couponDTO1.getGet_time());
            trackVO1.setState(couponDTO1.getState());
            trackVO1.setUsed_time(couponDTO1.getUsed_time());
            trackVO1.setOrder_number(couponDTO1.getOrder_number());

            list.add(trackVO1);
        }
        return Result.success(list);
    }

    @ApiOperation(value = "根据订单编号查询优惠券")
    @PostMapping("/findByNumber")
    @ResponseBody
    public Result<TrackVO> findByNumber(String number){
        CouponDTO couponDTO = new CouponDTO();
        TrackVO trackVO = new TrackVO();
        couponDTO = couponFeignClient.findByNumber(number);
        trackVO.setId(couponDTO.getId());
        trackVO.setCoupon_id(couponDTO.getCoupon_id());
        trackVO.setUser_id(couponDTO.getUser_id());
        trackVO.setType(couponDTO.getType());
        trackVO.setGet_time(couponDTO.getGet_time());
        trackVO.setState(couponDTO.getState());
        trackVO.setUsed_time(couponDTO.getUsed_time());
        trackVO.setOrder_number(couponDTO.getOrder_number());
        return Result.success(trackVO);
    }

    @ApiOperation(value = "根据状态查询优惠券2")
    @PostMapping("/findByState")
    @ResponseBody
    public Result<TrackVO> findByState(int state){
        CouponDTO couponDTO = new CouponDTO();
        TrackVO trackVO = new TrackVO();
        couponDTO = couponFeignClient.findByState(state);
        trackVO.setId(couponDTO.getId());
        trackVO.setCoupon_id(couponDTO.getCoupon_id());
        trackVO.setUser_id(couponDTO.getUser_id());
        trackVO.setType(couponDTO.getType());
        trackVO.setGet_time(couponDTO.getGet_time());
        trackVO.setState(couponDTO.getState());
        trackVO.setUsed_time(couponDTO.getUsed_time());
        trackVO.setOrder_number(couponDTO.getOrder_number());
        return Result.success(trackVO);
    }
}
